package utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;

public class WordThread implements Runnable {
	private final static File wordsFile = new File(WordThread.class.getResource("/").getPath()+"viewCount.txt");
	private String word ;
	public WordThread(String newword){
		word = newword;
	}
	
	public void run() {
			  FileWriter writer = null;  
		        try {     
		            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件     
		            writer = new FileWriter(wordsFile, true);     
		            writer.write(word+"\r\n");       
		        } catch (IOException e) {     
		            e.printStackTrace();     
		        } finally {     
		            try {     
		                if(writer != null){  
		                    writer.close();     
		                }  
		            } catch (IOException e) {     
		                e.printStackTrace();     
		            }     
		        }   
	}
	
	public static synchronized  List<String>  wordsReload(){
		List<String> words = new ArrayList<String>();
        try{
            LineIterator lines = FileUtils.lineIterator(wordsFile, "utf-8");
            while(lines.hasNext()){
                String line = lines.nextLine();
                if(StringUtils.isNotBlank(line))
                    words.add(StringUtils.trim(line).toLowerCase());
            }
            FileUtils.writeLines(wordsFile, "UTF-8", new ArrayList());
        }catch(IOException e){
            e.printStackTrace();
        }
        return words;
}

}
